package com.txh.blog.service.Impl;

import com.github.pagehelper.PageHelper;
import com.txh.blog.bean.*;
import com.txh.blog.dao.BlogDao;
import com.txh.blog.service.BlogService;
import com.txh.blog.util.MarkdownUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BlogServiceImpl implements BlogService{
    @Autowired
    private BlogDao blogDao;

    @Override
    public Blog getBlog(Long id) {
        return blogDao.getBlog(id);
    }

    @Override
    public List<Blog> findAll(int page,int pageSize) {
        PageHelper.startPage(page,pageSize);
        return blogDao.findAll();
    }

    @Override
    public List<BlogList> search(int page,int pageSize,SearchBlog searchBlog) {
        PageHelper.startPage(page,pageSize);
        return blogDao.search(searchBlog);
    }

    @Override
    public void saveBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        blog.setCreateTime(new Date());
        blog.setViews(0);
        blogDao.saveBlog(blog);
        List<Tags> tags = blog.getTags();
        BlogAndTag blogAndTag = null;
        for (Tags tag : tags) {
            blogAndTag = new BlogAndTag(blog.getTagIds(),blog.getId());
            blogDao.saveBlogAndTag(blogAndTag);
        }
    }

    @Override
    public List<Blog> listBlog() {
        return blogDao.listBlog();
    }

    @Override
    public void transformRecommend(SearchBlog searchBlog) {
        if ("true".equals(searchBlog.getRecommend())) {
            searchBlog.setRecommend2(1);
        }else if("false".equals(searchBlog.getRecommend())){
            searchBlog.setRecommend2(0);
        }

    }

    @Override
    public void updateBlog(Blog blog) {
       blog.setUpdateTime(new Date());
        blogDao.updateBlog(blog);
    }

    @Override
    public void deleteBlog(Long id) {
       blogDao.deleteBlog(id);
    }

    @Override
    public List<FirstPageBlog> getFirstPageBlog(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        return blogDao.getFirstPageBlog();
    }

    @Override
    public List<Blog> finByTagId() {
        return blogDao.finByTagId();
    }

    @Override
    public List<getAllRecommendBlog> getAllRecommendBlog() {
        return blogDao.getAllRecommendBlog();
    }

    @Override
    public List<FirstPageBlog> getSearchBlog(int page,int PageSize,String query) {
        return blogDao.getSearchBlog(query);
    }

    @Override
    public Blog getDetailedBlog(Long id) {
        Blog detailedBlog = blogDao.getDetailedBlog(id);
        String content = detailedBlog.getContent();
        detailedBlog.setContent(MarkdownUtils.markdownToHtmlExtensions(content));
        return detailedBlog;
    }

    @Override
    public Blog listId(Long id) {
        return blogDao.listId(id);
    }

    @Override
    public List<FirstPageBlog> getByTypeId(Long typeId, int page, int pageSize) {
        PageHelper.startPage(page,pageSize);
        return blogDao.getByTypeId(typeId);
    }

    @Override
    public List<FirstPageBlog> getByTagId(Long typeId) {
        return blogDao.getByTagId(typeId);
    }

    @Override
    public Map<String, List<Blog>> archives() {
        List<String> years=blogDao.findByYear();
        Map<String,List<Blog>> map=new HashMap<>();
        for (String year:years) {
           map.put(year,blogDao.selectByYear(year));
        }
        return map;
    }

    @Override
    public String blogCount() {
        return blogDao.blogCount();
    }


}
